package com.nm.orm.convert;

import com.nm.commons.util.StringUtil;
import com.nm.orm.pojo.*;
import com.nm.orm.pojo.out.ArticleBackPageOutput;
import com.nm.orm.pojo.out.ArticleInnerCategoryOutput;
import com.nm.orm.pojo.out.ArticleInnerLabelOutput;
import com.nm.orm.pojo.out.ArticleInnerUserOutput;
import org.assertj.core.util.Collections;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2017/3/6 0006.
 */
public class ArticleConvert {

    /**
     * 文章后台分页查询转换器
     *
     * @param articles
     * @return
     */
    public static List<ArticleBackPageOutput> conv(List<ArticleEntity> articles) {
        if (Collections.isNullOrEmpty(articles)) {
            return null;
        }

        List<ArticleBackPageOutput> backArticles = new ArrayList<ArticleBackPageOutput>();

        for (ArticleEntity article : articles) {
            ArticleBackPageOutput backArticle = new ArticleBackPageOutput();
            backArticle.setId(article.getId());
            backArticle.setContent(article.getContent());
            backArticle.setName(article.getName());
            backArticle.setHit(article.getHit());

            UserEntity user = article.getUser();
            if (!StringUtil.isNull(user)) {
                ArticleInnerUserOutput innerUser = new ArticleInnerUserOutput(user.getId(), user.getName());
                backArticle.setUser(innerUser);
            }

            CategoryEntity category = article.getCategory();
            if (!StringUtil.isNull(category)) {
                ArticleInnerCategoryOutput innerCategory = new ArticleInnerCategoryOutput(category.getId(), category.getName());
                backArticle.setCategory(innerCategory);
            }

            Set<ArticleLabelRefEntity> articleArtLabelRefs = article.getArtLabelRefs();
            if (!Collections.isNullOrEmpty(articleArtLabelRefs)) {
                List<ArticleInnerLabelOutput> innerLabels = new ArrayList<ArticleInnerLabelOutput>();
                for (ArticleLabelRefEntity alrEntity : articleArtLabelRefs) {
                    LabelEntity label = alrEntity.getLabel();
                    ArticleInnerLabelOutput innerLabel = new ArticleInnerLabelOutput(label.getId(), label.getName());
                    innerLabels.add(innerLabel);
                }
                backArticle.setLabels(innerLabels);
            }
            backArticles.add(backArticle);
        }
        return backArticles;
    }
}
